<?php

class Location extends CActiveRecord {
    /**
     * The followings are the available columns in table 'www_locations':
     * @var integer $id
     * @var string $name
     * @var integer $parent_id
     * @var integer $order_pos
     * @var integer $type
     */

    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{locations}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
        return array(
            array('parent_id, name, type, order_pos', 'required'),
            array('parent_id, type, order_pos', 'numerical', 'integerOnly' => true),
            array('name', 'length', 'max' => 50),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'name' => Yii::t('location', 'name'),
            'parent_id' => Yii::t('location', 'parent_id'),
            'order_pos' => Yii::t('location', 'order_pos'),
            'fullname' => Yii::t('location', 'fullname'),
            'type' => Yii::t('location', 'type'),
        );
    }

    const TYPE_COUNTRY = 0;
    const TYPE_PROVINCE = 1;
    const TYPE_CITY = 2;
    const TYPE_REGION = 3;

    // 类型列表
    public function getTypeOptions() {
        return array(
            self::TYPE_COUNTRY   => Yii::t('location', 'type_country'),
            self::TYPE_PROVINCE => Yii::t('location', 'type_province'),
            self::TYPE_CITY => Yii::t('location', 'type_city'),
            self::TYPE_REGION => Yii::t('location', 'type_region'),
        );
    }

    // 获取省市地区类型
    public function getTypeTextFormat() {
        $options = $this->typeOptions;
        return (isset($options[$this->type]))
            ? $options[$this->type]
            : 'unknown';
    }

    // 全称
    public function getFullname() {

    }

    /**
     * 返回 CTreeView 所需数据
     * @param <integer> $parent_id
     * @return <string>
     */
    public function getCTreeViewDatas($parent_id = 1) {
        $datas = $this->generateCTreeViewDatas($parent_id);
        return CArrayExt::toTree($datas, 'id', 'parent', 'childrens', $refs);
    }

    // 遍历节点属于 $parent_id 的所有子节点，供 getCTreeViewDatas 生成返回
    // CTreeView 所需数据
    private $_CTreeView_datas;
    public function generateCTreeViewDatas($parent_id = 1) {
        $criteria = new CDbCriteria;
        $criteria->condition = 'parent_id = ' . $parent_id;
        $criteria->order = 'order_pos ASC';
        $locations = Location::model()->findAll($criteria);
//        CVarDumper::dump($nodes, 20, true);
//        $this->_CTreeView_datas[$parent_id] = array(
//            'id' => $parent_id,
//            'text' => "<span>xxx</span>",
//            'expanded' => true,
//            'classes' => 'important',
//            'parent' => $parent_id,
//        );
        foreach ($locations as $j => $location) {
//            echo 'xx';
            $location_id = $location->id;
            if ($this->hasChild($location_id)) {
                $this->_CTreeView_datas[$location_id] = array(
                    'id' => $location_id,
                    'text' => "<span>{$location->name}</span>",
                    'expanded' => true,
                    'classes' => 'important',
                    'parent' => $location->parent_id,
                    'order_pos' => $location->order_pos,
                );
                $this->generateCTreeViewDatas($location_id);
            } else {
                $this->_CTreeView_datas[$location_id] = array(
                    'id' => $location_id,
                    'text' => CHtml::link($location->name, 'http://www.yuandot.com'),
                    'expanded' => false,
                    'classes' => 'important',
                    'parent' => $location->parent_id,
                    'order_pos' => $location->order_pos,
                );
            }
        }

        return $this->_CTreeView_datas;
    }

    /**
     * 判断是否有子节点
     * @param integer $node_id
     * @return boolean
     */
    private static function hasChild($node_id) {
        $criteria = new CDbCriteria;
        $criteria->condition = "parent_id = {$node_id}";
        return (Location::model()->count($criteria)) ? true : false;
    }
}